<?php
namespace app\supplier\controller;

use think\Controller;
use think\captcha\Captcha;
use app\erp\model\Member;
use think\Cookie;
use app\erp\model\Memberdb;
use app\erp\model\Systemdb;
use app\erp\model\Orderdb;
use think\Db;

class Login extends Controller
{

    public function index()
    {
        $request = request();
        if($request->server('REQUEST_SCHEME') != 'https'){
            header("Location: https://tailongji.dixintong.com/supplier/login/index.html");
            exit();
        }
        if ($this->__system() === true) {
            if (session('supplier_id')) {
                $this->redirect('index/index');
            }
            $username = '';
            if (Cookie::has('username_supplier')) {
                $username = Cookie::get('username_supplier');
            }
            $this->assign('username', $username);
            return $this->fetch();
        }
    }

    /**
     *
     * @todo 登录
     */
    public function ajax_login()
    {
        $request = request();
        $param = $request->param();
        $param['is_supplier'] = 1;
        // 检查验证码
        if (! captcha_check($param['verify'], 'login')) {
            $this->error('验证码错误');
        }
        $member = new Memberdb();
        if(isset($param['is_supplier'])){
        	$get_user = $member->username($param['username'])
        	->password($param['password'])
        	->siteId(117)
        	->isSupplier($param['is_supplier'])
        	->find();
        } else {
        	$get_user = $member->username($param['username'])
        	->password($param['password'])
        	->siteId(117)
        	->find();
        }
        
        if ($get_user) {
            if ($get_user['is_manage'] != 2) {
                $this->error('请使用供应商账号登录');
            }
            if ($get_user['state'] != 1) {
                $this->error('该账户已被冻结');
            }
            // 校验指纹
            if(getSiteConfig('OPEN_FINGERPRINT') == '是'){
                $chk_finger = Db::name('member_fingerprint')->where('member_id',$get_user['member_id'])
                    ->where('fingerprint',$param['fingerprint'])
                    ->find();
                if(!$chk_finger){
                    $this->error('指纹错误，请联系管理员');
                }
            }
            // 指纹校验结束
            if (isset($param['remember']) && $param['remember'] == 'on') {
                Cookie::forever('username_supplier', $param['username']);
            } else {
                cookie('username_supplier', null);
            }
            $member->supplierLogin($get_user['member_id']);
            $this->success('登录成功', url('index/index'));
        } else {
            $this->error('用户名或密码错误');
        }
    }

    /**
     *
     * @todo 退出登录
     */
    public function ajax_logout()
    {
        if (session('supplier_id')) {
            $member = new Memberdb();
            $member->logout(session('supplier_id'));
        }
        $this->redirect('login/index');
    }
    protected function __system()
    {
        $controller = init_file('controller');
        $model = init_file('model');
        $validate = init_file('validate');
        $str = $controller . $model . $validate;
        $systemdb = new Systemdb();
        $systemdb->param['system_json'] = $str;
        return $systemdb->getSys();
    }

    /**
     *
     * @todo 验证码
     * @return \think\Response
     */
    public function verify()
    {
        $config = [
            'fontSize' => 36,
            'length' => 4,
            'useNoise' => false,
            'useImgBg' => false,
            'useCurve' => false,
            'bg' => [
                255,
                255,
                255
            ]
        ];
        $captcha = new Captcha($config);
        return $captcha->entry('login');
    }
}